package net.cyndeline.GUI.rendering

import net.cyndeline.GUI.graphics.Representation
import net.cyndeline.misc.Bag
import net.cyndeline.scalarlib.entityComponent.miranda.Entity

/**
 * Maps the graphics of every entity based on their id.
 */
class GraphicsContainer {
  private val graphics = new Bag[Representation]()

  /** Returns the graphics representation of an entity, if it has one. */
  def getGraphic(entity: Entity): Option[Representation] = graphics.get(entity.id)

  /** Sets the graphical representation for an entity that doesn't already have one. */
  def setGraphic(entity: Entity, rep: Representation): Unit = {
    if (graphics.get(entity.id).isDefined)
      throw new Error("Entity " + entity + " already have a graphical representation: " + graphics.get(entity.id).get + ", cannot give it " + rep)

    graphics.synchronized {
      graphics.set(entity.id, rep)
    }
  }

  /**
   * Removes a graphical representation. Do not use when hiding objects on screen (that should be managed in
   * the renderer), only when entities no longer should be graphically representated (such as when they are
   * deleted).
   */
  def deleteGraphic(entity: Entity): Unit = {
    graphics.synchronized {
      graphics.remove(entity.id)
    }
  }

}
